package com.lgh.hotel.service.impl;

import com.lgh.hotel.dao.mapper.RoomTypeMapper;
import com.lgh.hotel.dao.pojo.RoomType;
import com.lgh.hotel.service.RoomTypeService;
import com.lgh.hotel.service.TableServer;
import com.lgh.hotel.vo.Result;
import com.lgh.hotel.vo.params.RoomTypeParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomTypeServiceImpl implements RoomTypeService {
    @Autowired
    RoomTypeMapper roomTypeMapper;

    @Autowired
    TableServer tableServer;

    @Override
    public RoomType findRoomTypeById(Long roomType) {
        return roomTypeMapper.selectById(roomType);
    }

    @Override
    public Result findRoomTypeAll() {
        List<RoomType> roomTypes = roomTypeMapper.selectList(null);
        return Result.succ(roomTypes);
    }

    @Override
    public Result findRoomTypeByIdAll(Long id) {
        return tableServer.findTableRoomTypeByIdAll(id);
    }

    @Override
    public Result addRoomType(RoomTypeParams roomTypeParams) {
        RoomType roomType = new RoomType();
        BeanUtils.copyProperties(roomTypeParams,roomType);
        return Result.succ(roomTypeMapper.insert(roomType));
    }

    @Override
    public Result deleteRoomType(Long id) {

       boolean a = tableServer.findTableRoomTypeState(id);

       if (a){
           return Result.succ(roomTypeMapper.deleteById(id));
       }

       return Result.fail("已有住户入住 不能删除！");
    }

    @Override
    public Result updateRoomType(RoomType roomType) {
        return Result.succ(roomTypeMapper.updateById(roomType));
    }
}
